package com.yumeng.framework.persistence.mybatis.plugins.dynamictable.parser;

import com.yumeng.common.data.constants.DateConstants;
import com.yumeng.common.exception.YumengException;
import com.yumeng.common.helper.AssertHelper;
import com.yumeng.common.helper.LocalDateHelper;
import com.yumeng.common.helper.StringHelper;
import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.DynamicTableNameMode;
import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.TableNameDateSuffixType;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 按日期分表处理器
 *
 * @author wxd
 * @date 2022/9/1 10:14
 */
public class DateTableNameParser extends AbstractTableNameMapParser<String, TableNameDateSuffixType> {

    private final String dateSeparator = "#_#";

    public DateTableNameParser(Map<String, TableNameDateSuffixType> tableMap) {
        super(tableMap, DynamicTableNameMode.DATE);
    }

    public void setParam(String tableName, String kssj, String jssj, int tableCount) {
        AssertHelper.assertIsTrue(StringUtils.isNotBlank(kssj) && StringUtils.isNotBlank(jssj), "kssj或者jssj为空");
        String dateParam = kssj + dateSeparator + jssj;
        setParam(tableName, dateParam, tableCount);
    }

    @Override
    protected String doDynamicTableName(String sql, String tableName, String param) {
        String dateSuffix = analysisDateParam(param, getTableMapVal(tableName));
        return tableName + "_" + dateSuffix;
    }

    /**
     * 解析日期参数
     * @param dateParam
     * @param type
     * @return
     */
    private String analysisDateParam(String dateParam, TableNameDateSuffixType type){
        String dateSuffix;
        if (dateParam.contains(dateSeparator)){
            String[] tempArray = StringHelper.stringToArray(dateParam, dateSeparator);
            if (tempArray == null || tempArray.length != 2){
                throw new YumengException("dateParam 解析异常：%s", dateParam);
            }
            String kssj = tempArray[0];
            String jssj = tempArray[1];
            String kssjSuffix = getDateSuffix(kssj, type.getFormat());
            String jssjSuffix = getDateSuffix(jssj, type.getFormat());
            if (!kssjSuffix.equals(jssjSuffix)){
                throw new YumengException("查询范围不在同一个分组（%s）内：%s~%s", type.getDesc(), kssj, jssj);
            }
            dateSuffix = kssjSuffix;
        }else{
            dateSuffix = getDateSuffix(dateParam, type.getFormat());
        }
        return dateSuffix;
    }

    /**
     * 根据日期字符串，转换成指定格式的日期字符串
     * @param dateStr
     * @param tarFmt
     * @return
     */
    private String getDateSuffix(String dateStr, String tarFmt){
        String dateSuffix;
        if (DateConstants.YYYY_MM_DD_LEN.equals(dateStr.length())){
            dateSuffix = LocalDateHelper.formatDateStr(dateStr, tarFmt);
        }else if (DateConstants.YYYY_MM_DD_HH_MM_SS_LEN.equals(dateStr.length())){
            dateSuffix = LocalDateHelper.formatDateTimeStr(dateStr, tarFmt);
        }else {
            throw new YumengException("dateStr长度异常：%s", dateStr);
        }
        return dateSuffix;
    }
}
